package org.testis.gui.mvc.controller.dynamicmap;

import org.testis.orm.classes.T_bed_firmware;
import org.testis.utils.middleware.DynamicMap;

import org.testis.gui.mvc.controller.*;
import org.testis.gui.mvc.model.dynamicmap.LoadDynamicMapModel;
import org.testis.gui.mvc.view.dynamicmap.LoadDynamicMapView;
import org.testis.utils.SortAlphabetically;


/**
 *
 * @author Cahit Emir Erel
 */
public class LoadDynamicMapController extends AbstractController
{

    public static final String SELECT_FIRMWARE = "Firmware";
    public static final String SELECT_DUT = "Dut";
    public static final String SHOW_LOADER = "LoaderVisibility";
    public static final String SHOW_DESCRIPTION = "Description";


    public LoadDynamicMapController()
    {
        super();
        DynamicMapControllerList.newInstance().setLoadDynamicMapController(this);
    }


    public void startLoader()
    {
        initialize();
        setModelProperty(SHOW_LOADER,
                         true);
    }


    public void initialize()
    {
        LoadDynamicMapModel loadDynamicMapModel = new LoadDynamicMapModel();
        LoadDynamicMapView loadDynamicMapView = new LoadDynamicMapView(
            DynamicMapControllerList.newInstance().getParentFrame(),
            true);
        super.addModel(loadDynamicMapModel);
        super.addView(loadDynamicMapView);
        loadDynamicMapView.pack();
    }


    public void operationLoad()
    {
        String result = (String) getModelProperty(SELECT_FIRMWARE);
        String result2 = (String) getModelProperty(SELECT_DUT);
        if (result != null && !result.equals("") && result2 != null &&
            !result2.equals("")) {

            DynamicMapControllerList.newInstance().getDynamicMapTreeController().
                operationLoadTreeFromDatabase(result,
                                              result2);
        }
    }


    public void operationSelectFirmware(String firmware,
                                         String dut)
    {
        setModelProperty(SELECT_FIRMWARE,
                         firmware);

        setModelProperty(SHOW_DESCRIPTION,
                         getFirmwareDetails(firmware,
                                            dut));
    }


    public void operationSelectDut(String dut)
    {
        setModelProperty(SELECT_DUT,
                         dut);
        setModelProperty(SHOW_DESCRIPTION,
                         "");
    }


    private String getFirmwareDetails(String name,
                                       String dut)
    {
        T_bed_firmware firmware = DynamicMap.newInstance().getFirmwareByNameAndDutName(name,dut);
        if (firmware != null) {
            String result = "Id:" + firmware.getId();
            result += "\nName:" + firmware.getName();
            result += "\nDescription:" + firmware.getDescription();
            result += "\nDevice Definition:" + firmware.getDevice().
                getName();
            result += "\nDevice Abstract Name:" + firmware.getDevice().
                getAbstract().getName();
            result += "\nDynamic Map Tree Id:" + firmware.getDeviceMapId();
            return result;
        }
        else {
            return "Firmware not found!";
        }
    }


    public String[] getFirmwareNames(String dut)
    {
        String[] temp = DynamicMap.newInstance().getFirmwaresByDutName(dut);
        return SortAlphabetically.sortAlphabetically(temp,false);
    }


    public String[] getDutNames()
    {
        String[] temp = DynamicMap.newInstance().getDuts();
        return SortAlphabetically.sortAlphabetically(temp,false);
    }


    public void operationCloseWindow()
    {
        this.cleanAll();
    }
}
